<template>
  <view class="container">
    <image src="../../../static/img/databankbgc.png" mode="aspectFill" style="width: 100%; height: 340rpx"></image>
    <view class="title" :style="[top]">资料库</view>

    <swiper :autoplay="true" :interval="3000" :duration="1000" style="position: absolute; top: 170rpx; left: 20rpx; width: 710rpx; height: 300rpx">
      <swiper-item class="banner" v-for="item in imglist" :key="item.id" @click="goarticle2(item.content)">
        <image :src="imgUrl + item.url" mode="aspectFill"></image>
      </swiper-item>
    </swiper>
    <!-- 选择 -->
    <view class="choose">
      <view style="text-align: center" @click="goteach(index, item.id)" v-for="(item, index) in category" :key="item.id">
        <image :src="imgUrl + item.image" mode="aspectFill" style="width: 100rpx; height: 100rpx"></image>
        <view style="font-size: 26rpx; font-family: PingFang SC, PingFang SC; font-weight: 500; color: #222222">{{ item.name }}</view>
      </view>
    </view>
    <!-- 文章 -->
    <scroll-view id="scrollbox" scroll-y="true" :style="{ width: '100%', 'box-sizing': 'border-box', height: scrollheight + 'px' }" @scrolltolower="tolower">
      <view class="articlebox" v-for="item in list" :key="item.id" @click="goarticle(item.id)">
        <image :src="imgUrl + item.pic" mode="aspectFill" v-if="item.pic != ''" style="width: 710rpx; height: 280rpx"></image>
        <view style="font-size: 28rpx; font-family: PingFang SC, PingFang SC; font-weight: bold; color: #222222; padding-left: 30rpx; margin-top: 10rpx">
          {{ item.title }}
        </view>
      </view>
    </scroll-view>

    <view style="width: 750rpx; height: 100rpx"></view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      imgUrl: '',
      isiphone: false,
      top: {
        paddingTop: '0rpx'
      },
      imglist: [], // 轮播图数据
      category: [], // 类目
      page: 1,
      list: [], // 文章列表
      scrollheight: '' // 可用的高度
    };
  },
  methods: {
    // 加载更多
    tolower() {
      this.page++;
      this.getlist();
    },
    // 跳转轮播图详情
    goarticle2(txt) {
      uni.navigateTo({
        url: '/pages/swiperdetail/swiperdetail?txt=' + encodeURIComponent(txt)
      });
    },
    // 获取轮播图
    async getswiperlist() {
      const res = await uni.$http.post('/news/banner', {
        type: 'teacher'
      });
      // console.log('轮播图数据', res.data.data);
      this.imglist = res.data.data.list;
    },
    // 获取资料库的分类
    async gettype() {
      const res = await uni.$http.post('/news/category', {
        type: 'teacher'
      });
      console.log('类目', res.data.data.list);
      this.category = res.data.data.list;
    },
    // 跳转到教学方法
    goteach(index, id) {
      if (index == 0) {
        uni.navigateTo({
          url: '/teacher/jxff/jxff?id=' + id
        });
      } else if (index == 1) {
        // 跳转到案例分享
        uni.navigateTo({
          url: '/teacher/alfx/alfx?id=' + id
        });
      } else if (index == 2) {
        // 跳转到资料下载
        uni.navigateTo({
          url: '/teacher/zlxz/zlxz?id=' + id
        });
      }
    },
    // 跳转到文章详情
    goarticle(id) {
      uni.navigateTo({
        url: '/parents/articledetail/articledetail?id=' + id
      });
    },
    // 获取资料库的数据列表
    async getlist() {
      const res = await uni.$http.post('/news/articleList', {
        page: this.page,
        type: 'teacher',
        title: '',
        category_id: '5'
      });
      if (res.data.data.list.length == 0) {
        return uni.showToast({
          title: '数据已加载完毕',
          icon: 'none'
        });
      } else {
        this.list.push(...res.data.data.list);
      }
      // this.list = res.data.data.list;
      // console.log('列表数据', this.list);
    }
  },
  created() {
    // 获取轮播图
    this.getswiperlist();
    // 获取资料库的分类
    this.gettype();
    // 获取数据列表
    this.getlist();
    this.imgUrl = this.imgurl;
    const app = getApp();
    this.isiphone = app.$vm.isIPhone;
    if (this.isiphone) {
      this.top.paddingTop = app.$vm.liuhai + 'rpx';
    }
  },
  mounted() {
    // 获取可用的高度
    uni.getSystemInfo({
      success: (res) => {
        console.log('屏幕', res);
        const query = uni.createSelectorQuery().in(this);
        console.log(query, '----');

        query
          .select('#scrollbox')
          .boundingClientRect((data) => {
            console.log('可用高度的数据', data);
            this.scrollheight = res.windowHeight - data.top;
            console.log('可用的高度-----', this.scrollheight);
          })
          .exec();
      }
    });
  }
};
</script>

<style lang="scss" scoped>
.container {
  box-sizing: border-box;
  .title {
    position: absolute;
    top: 65rpx;
    left: 315rpx;
    font-size: 32rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: bold;
    color: #ffffff;
  }
  .banner {
    box-sizing: border-box;

    width: 710rpx;
    height: 300rpx;
    image {
      width: 100%;
      height: 100%;
      border-radius: 20rpx;
    }
  }
  .choose {
    display: flex;
    justify-content: space-around;
    margin-top: 150rpx;
  }
  .articlebox {
    box-sizing: border-box;
    width: 710rpx;
    height: 360rpx;
    margin: 30rpx 20rpx;
    background: #ffffff;
    box-shadow: 0rpx 4rpx 4rpx 2rpx rgba(191, 191, 191, 0.16);
    border-radius: 20rpx 20rpx 20rpx 20rpx;
  }
}
</style>
<style>
page {
  background-color: #ffffff;
}
</style>
